//
//  LYAuthorizedMaster.m
//  Yihubang
//
//  Created by xmy on 16/9/13.
//  Copyright © 2016年 jinghao. All rights reserved.
//

#import "LYAuthorizedMaster.h"
#import <AddressBookUI/AddressBookUI.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <AVFoundation/AVFoundation.h>
#import "AppDelegate.h"

#define kAPPName [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"]

@implementation LYAuthorizedMaster

#pragma mark -
+(BOOL)checkAuthority:(AVAuthorizationStatus)_status{
    return (_status == AVAuthorizationStatusAuthorized) || (_status == AVAuthorizationStatusNotDetermined);
}
+(void)showAlertController:(AuthorizedFinishBlock)_block device:(NSString *)_device{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] * 100 < 800 ) {
        UIAlertView *_alertc = [[UIAlertView alloc]initWithTitle:@"没有权限" message:[NSString stringWithFormat:@"请开启‘%@’对 %@ 的使用权限,设置->隐私->%@->%@",kAPPName,_device,_device,kAPPName] delegate:self cancelButtonTitle:@"取消" otherButtonTitles: nil];
        [_alertc show];
    }else{
        UIAlertController *_alertC = [UIAlertController alertControllerWithTitle:@"没有权限" message:[NSString stringWithFormat:@"请开启‘%@’对 %@ 的使用权限",kAPPName,_device] preferredStyle:UIAlertControllerStyleAlert];
        [_alertC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
        [_alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
        }]];
        [SharedAppDelegate.window.rootViewController presentViewController:_alertC animated:YES completion:_block];
    }
}

#pragma mark - 摄像头权限
+(BOOL)checkCameraAuthority{
    return [self checkAuthority:[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]];
}
+(void)cameraAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail;{
    if ([self checkCameraAuthority]) {
        if (_success) {
            _success();
        }
    }else{
        [self showAlertController:_fail device:@"相机"];
    }
}

#pragma mark - 麦克风权限
+(BOOL)checkAudioAuthority{
    return [self checkAuthority:[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio]];
}
+(void)audioAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail{
//    if ([self checkAudioAuthority]) {
//        if (_success) {
//            _success();
//        }
//    }else{
//        [self showAlertController:_fail device:@"麦克风"];
//    }
    
    [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
        if (granted) {
            _success();
        } else {
            // 用户不同意获取麦克风
            [self showAlertController:_fail device:@"麦克风"];
        }
    }];
}

#pragma mark - 相册权限
+(BOOL)checkAlbumAuthority{
    return [ALAssetsLibrary authorizationStatus] == kCLAuthorizationStatusRestricted|| [ALAssetsLibrary authorizationStatus] ==ALAuthorizationStatusDenied;
}
+(void)albumAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail;
{
    if (![self checkAlbumAuthority]) {
        if (_success) {
            _success();
        }
    }else{
        [self showAlertController:_fail device:@"照片"];
    }
}

#pragma mark - 位置权限
+(BOOL)checkLocationAuthority {
    return SharedAppDelegate.canLocation;
}
+(void)locationAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail{
    
    if ([self checkLocationAuthority]) {
        if (_success) {
            _success();
        }
    }else{
        [self showAlertController:_fail device:@"位置"];
    }
}

#pragma mark - 推送通知权限
+(BOOL)checkPushNotificationAuthority {
    return [[UIApplication sharedApplication] currentUserNotificationSettings].types != UIUserNotificationTypeNone;
}
+(void)pushNotificationAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail{
    
    if ([self checkPushNotificationAuthority]) {
        if (_success) {
            _success();
        }
    }else{
        [self showAlertController:_fail device:@"通知"];
    }
}

#pragma mark - 通讯录权限
+(BOOL)checkAddressBookAuthority {
    return ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized || ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined;
}
+(void)AddressBookAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail{
    
    if ([self checkAddressBookAuthority]) {
        if (_success) {
            _success();
        }
    }else{
        [self showAlertController:_fail device:@"通讯录"];
    }
}
#pragma mark - 是否认证
+ (BOOL)doCertification{
    return [UserClient sharedUserClient].isMaster;
}
+(void)CertificationSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail{
    
    if ([self doCertification]) {
        if (_success) {
            _success();
        }
    }else{
        UIStoryboard *story = [UIStoryboard storyboardWithName:@"Mine" bundle:[NSBundle mainBundle]];
        UIViewController *myView = [story instantiateViewControllerWithIdentifier:@"CertificationViewController"];
        [[MasterUrlManager shareMasterUrlManager].currentNav pushViewController:myView animated:YES];
    }
}

#pragma mark - 是否登陆
+ (BOOL)doLogin
{
    return [UserClient sharedUserClient].rawLogin;
}
+(void)LoginCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail{
    
    if ([self doLogin]) {
        if (_success) {
            _success();
        }
        }else{
        UIViewController *vc = [MasterUrlManager getViewControllerWithUrl:URL_MasterLoginRoot];
        [((AppDelegate *)[UIApplication sharedApplication].delegate).window.rootViewController presentViewController:vc animated:TRUE completion:^{
        }];

    }
}
#pragma mark - 判断守护权限
+(void)OpenProductAuthorityCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail{
    @weakify(self);
    [self LoginCheckSuccess:^(void){
        @strongify(self);
        [self locationAuthorityCheckSuccess:^(void){
            [self audioAuthorityCheckSuccess:^(void){
                _success();
            } fail:nil];
        } fail:nil];
    
    } fail:nil];
}
#pragma mark - 判断吓阻和紧急权限
+(void)OpenScareAndEmergentCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail{
    @weakify(self);
    [self LoginCheckSuccess:^(void){
        @strongify(self);
        [self locationAuthorityCheckSuccess:^(void){
            [self audioAuthorityCheckSuccess:^(void){
                    _success();
            } fail:nil];
        } fail:nil];
        
    } fail:nil];

}
#pragma mark - 发布图文

+(void)PublishPictureCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail{
    @weakify(self);
    [self LoginCheckSuccess:^(void){
        @strongify(self);
        [self CertificationSuccess:^(void){
            [self locationAuthorityCheckSuccess:^(void){
                _success();
            } fail:nil];
        } fail:nil];
    } fail:nil];
}

#pragma mark - 发布语音

+(void)PublishAudioCheckSuccess:(AuthorizedFinishBlock)_success fail:(AuthorizedFinishBlock)_fail{
    @weakify(self);
    [self LoginCheckSuccess:^(void){
        @strongify(self);
        [self CertificationSuccess:^(void){
            [self locationAuthorityCheckSuccess:^(void){
                [self audioAuthorityCheckSuccess:^(void){
                    _success();
                } fail:nil];
            } fail:nil];
            
        } fail:nil];
    } fail:nil];
}
@end
